package com.hhsjdesign.test;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.hhsjdesign.headerpullrefresh.R;

import java.util.ArrayList;
import java.util.List;

public class ListFragment extends Fragment {
    private int size = 0;
    private String title;

    private ListFragment(String title) {
        this(title, 20);
    }

    private ListFragment(String mTitle, int size) {
        this.title = mTitle;
        this.size = size;
    }

    public static ListFragment newInstance(String mTitle, int size) {
        return new ListFragment(mTitle, size);

    }


    private View content;
    private RecyclerView recyclerView;
    private MAdapter adapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        content = inflater.inflate(R.layout.fmt_list, null);
        initViews(savedInstanceState);
        return content;
    }

    private void initViews(Bundle savedInstanceState) {
        recyclerView = findViewById(R.id.recycler_view);
        recyclerView.setLayoutManager(new GridLayoutManager(getActivity(), 2, GridLayoutManager.VERTICAL, false));
        List<String> datas = new ArrayList<>();
        for (int i = 0; i < size; i++) {
            datas.add(title + ":" + i);
        }
        adapter = new MAdapter(getContext(), title, datas);

        recyclerView.setAdapter(adapter);
        adapter.notifyDataSetChanged();

    }

    private <T extends View> T findViewById(@IdRes int id) {
        return (T) content.findViewById(id);
    }

    private static class MAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

        private String mTitle;
        private Context mContext;
        private List<String> mDatas;

        public MAdapter(Context context, String title, List<String> datas) {
            mContext = context;
            mTitle = title;
            this.mDatas = datas;
        }

        @NonNull
        @Override
        public MViewHoler onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            View view = LayoutInflater.from(mContext).inflate(R.layout.layout_item_text, null);
            return new MViewHoler(view);
        }

        @Override
        public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
            ((MViewHoler) holder).textView.setText(mTitle + ":" + position);
        }

        @Override
        public int getItemCount() {
            return mDatas.size();
        }
    }


    private static class MViewHoler extends RecyclerView.ViewHolder {

        TextView textView;

        MViewHoler(@NonNull View itemView) {
            super(itemView);
            textView = itemView.findViewById(R.id.text);
        }
    }

}
